package service

import (
	nodeOperator "ServiceEdge/controllers/src/node_operator"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type EditLabelToNodeInfo struct {
	NodeName string `json:"nodename"`
	Key      string `json:"key"`
	Value    string `json:"value"`
}
type RemoveLabelToNodeInfo struct {
	NodeName string `json:"nodename"`
	Key      string `json:"key"`
}
type NodeNameInfo struct {
	NodeName string `json:"nodename"`
}

func GetNodeMessage(c *gin.Context) {
	nodeMessage, err := nodeOperator.GetNodeMessage()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success",
		"message": nodeMessage,
	})
}

func DeleteNode(c *gin.Context) {
	var jsonData NodeNameInfo

	// 解析JSON参数
	if err := c.ShouldBindJSON(&jsonData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	logrus.Infof("Received JSON: %+v\n", jsonData)
	if jsonData.NodeName == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "nodename should not be empty"})
		logrus.Infof("nodename should not be empty")
		return
	}

	nodeName := jsonData.NodeName

	err := nodeOperator.DeleteNode(nodeName)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

func AddLabelToNode(c *gin.Context) {
	var jsonData EditLabelToNodeInfo

	// 解析JSON参数
	if err := c.ShouldBindJSON(&jsonData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	logrus.Infof("Received JSON: %+v\n", jsonData)
	if jsonData.NodeName == "" || jsonData.Key == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "nodename, key should not be empty"})
		logrus.Infof("nodename, key should not be empty")
		return
	}

	nodeName, key, value := jsonData.NodeName, jsonData.Key, jsonData.Value

	err := nodeOperator.AddLabelToNode(nodeName, key, value)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

func RemoveLabelFromNode(c *gin.Context) {
	var jsonData RemoveLabelToNodeInfo

	// 解析JSON参数
	if err := c.ShouldBindJSON(&jsonData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	logrus.Infof("Received JSON: %+v\n", jsonData)
	if jsonData.NodeName == "" || jsonData.Key == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "nodename, key should not be empty"})
		logrus.Infof("nodename, key should not be empty")
		return
	}

	nodeName, key := jsonData.NodeName, jsonData.Key

	err := nodeOperator.RemoveLabelFromNode(nodeName, key)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

func UpdateLabelOnNode(c *gin.Context) {
	var jsonData EditLabelToNodeInfo

	// 解析JSON参数
	if err := c.ShouldBindJSON(&jsonData); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	logrus.Infof("Received JSON: %+v\n", jsonData)
	if jsonData.NodeName == "" || jsonData.Key == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "nodename, key should not be empty"})
		logrus.Infof("nodename, key should not be empty")
		return
	}

	nodeName, key, value := jsonData.NodeName, jsonData.Key, jsonData.Value

	err := nodeOperator.UpdateLabelOnNode(nodeName, key, value)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"status": "fail"})
		return
	}
	// 返回成功响应
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}
